package com.monkey.demo.init;

import com.alibaba.fastjson.JSON;
import com.monkey.demo.entity.SysUser;
import org.apache.tomcat.jni.User;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * User: zhangqi
 * Date: 2024/7/15
 *
 * @Description:项目预热
 */

@Component
public class ApplicationInit implements ApplicationListener<ApplicationReadyEvent> {
    Map<String, InitMethod> map = new HashMap<>();

    /**
     * 定义一个代码块
     */
    {
        //map.put("init",this::initFastJson);
    }
    /**
     * @Description:  服务启动完成，执行的方法
     * @Param: applicationReadyEvent
     * @return: void
     * @Author: zhangqi
    */
    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        if(map.size()>0){
            map.forEach((key,value)->{
                value.doSomeThing();
            });
            System.out.println("服务启动完成调用完成事件方法!");
        }
    }

    /**
     * @Description: 定义一个调用测试方法
     *
     * @return: void
     * @Author: zhangqi
    */
    private void initFastJson(){
        SysUser sysUser = new SysUser();
        sysUser.setId(1L);
        sysUser.setUserName("ZQ");
        sysUser.setAge(12);
        String str = JSON.toJSONString(sysUser);
        System.out.println(str);
    }
    /**
     * @Description: 定义一个函数式接口
     *
     * @Author: zhangqi
    */

    interface InitMethod{
        void doSomeThing();
    }

}
